package com.glory.brepda.activity.technicalservice;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.adapter.RepairLogListAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.base.Constant;
import com.glory.brepda.entity.RepairFromEntity;
import com.glory.brepda.entity.RepairFromListEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DateUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_Utils;

import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class QCConfirmActivity extends BaseActivity {


    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.tvEqp)
    TextView tvEqp;
    @BindView(R.id.tvLocation)
    TextView tvLocation;
    @BindView(R.id.tvRepairType)
    TextView tvRepairType;
    @BindView(R.id.tvPriority)
    TextView tvPriority;
    @BindView(R.id.tvLotID)
    TextView tvLotID;
    @BindView(R.id.rvFaultLog)
    RecyclerView rvFaultLog;
    @BindView(R.id.tvQualified)
    TextView tvQualified;
    @BindView(R.id.tvUnQualified)
    TextView tvUnQualified;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.etComment)
    EditText etComment;
    @BindView(R.id.measurement)
    LinearLayout measurement;//测量
    @BindView(R.id.CLQualified)
    TextView CLQualified;
    @BindView(R.id.CLUnQualified)
    TextView CLUnQualified;
    @BindView(R.id.pushPullForce)
    LinearLayout pushPullForce;//推拉力
    @BindView(R.id.TLLQualified)
    TextView TLLQualified;
    @BindView(R.id.TLLUnQualified)
    TextView TLLUnQualified;
    @BindView(R.id.crater)
    LinearLayout crater;//弹坑
    @BindView(R.id.DKQualified)
    TextView DKQualified;
    @BindView(R.id.DKUnQualified)
    TextView DKUnQualified;
    @BindView(R.id.appearance)
    LinearLayout appearance;//外观
    @BindView(R.id.WGQualified)
    TextView WGQualified;
    @BindView(R.id.WGUnQualified)
    TextView WGUnQualified;
    @BindView(R.id.qc_btn)
    LinearLayout qc_btn;
    @BindView(R.id.qc_show)
    LinearLayout qc_show;
    @BindView(R.id.rbYes)
    RadioButton rbYes;
    @BindView(R.id.rbNo)
    RadioButton rbNo;

    private RepairFromListEntity.RepairData mData;
    private boolean initialSetValue = false;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_q_c_confirm;
    }

    @Override
    protected void initView() {
        setMainLayout(llMain);
        tvTitle.setText("QC检验");
        tvBaseSubmit.setText("维修详情");
        Bundle bundle = getIntent().getExtras();
        mData = (RepairFromListEntity.RepairData) bundle.getSerializable("REPAIRFROM");
        tvEqp.setText(mData.getREPAIRSHEETDATA().getEQUIPMENTID());
        tvLocation.setText(mData.getREPAIRSHEETDATA().getLOCATION());
        tvRepairType.setText(getType(mData.getREPAIRSHEETDATA().getREPAIRTYPE()));
        tvPriority.setText(mData.getREPAIRSHEETDATA().getPRIORITY());
        tvLotID.setText(mData.getREPAIRSHEETDATA().getLOTID());
        initRecyclerView(rvFaultLog, this);
        RepairLogListAdapter mRepairLogAdapter = new RepairLogListAdapter(mData.getREPAIRFORMHISLIST());
        mRepairLogAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mRepairLogAdapter.isFirstOnly(true);
        rvFaultLog.setAdapter(mRepairLogAdapter);
        if (mData.getREPAIRFORM().getSAMPLEFLAGE().equals("Y")){
            rbYes.setChecked(true);
        }else if(mData.getREPAIRFORM().getSAMPLEFLAGE().equals("N")){
            rbNo.setChecked(true);
        }
        initialSetValue = true;
        boolean qc_type = bundle.getBoolean("QC_SHOW");
        if (qc_type){
            qc_btn.setVisibility(View.VISIBLE);
            qc_show.setVisibility(View.GONE);
        }else {
            qc_btn.setVisibility(View.GONE);
            qc_show.setVisibility(View.VISIBLE);
            switch (getType(mData.getREPAIRSHEETDATA().getREPAIRTYPE())){
                case "待料8小时":
                    measurement.setVisibility(View.VISIBLE);
                    pushPullForce.setVisibility(View.VISIBLE);
                    crater.setVisibility(View.GONE);
                    appearance.setVisibility(View.GONE);
                    break;
                case "换工单":
                    measurement.setVisibility(View.GONE);
                    pushPullForce.setVisibility(View.GONE);
                    crater.setVisibility(View.GONE);
                    appearance.setVisibility(View.VISIBLE);
                    break;
                case "换产品":
                    measurement.setVisibility(View.VISIBLE);
                    pushPullForce.setVisibility(View.VISIBLE);
                    crater.setVisibility(View.VISIBLE);
                    appearance.setVisibility(View.GONE);
                    break;
                case "换材料":
                case "换Tooling":
                    measurement.setVisibility(View.GONE);
                    pushPullForce.setVisibility(View.GONE);
                    crater.setVisibility(View.VISIBLE);
                    appearance.setVisibility(View.GONE);
                    break;
            }
        }

    }

    private String getType(String repairtype) {
        String type = "";
        switch (repairtype) {
            case "normal":
                type = "一般故障";
                break;
            case "eqp":
                type = "设备故障";
                break;
            case "program":
                type = "程序问题";
                break;
            case "changeProducct":
                type = "换产品";
                break;
            case "changeMaterial":
                type = "换材料";
                break;
            case "changeTool":
                type = "换Tooling";
                break;
            case "changeWo":
                type = "换工单";
                break;
            case "waitingFor8Hour":
                type = "待料8小时";
                break;
            default:
                type = "未知错误";
                break;
        }
        return type;
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.tvQualified, R.id.tvUnQualified,R.id.tvBaseSubmit,R.id.CLQualified,R.id.CLUnQualified,R.id.TLLQualified,R.id.TLLUnQualified,R.id.DKQualified,R.id.DKUnQualified,R.id.WGQualified,R.id.WGUnQualified,R.id.rbYes,R.id.rbNo})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvQualified:
                showSubmitDialog(() -> requestQC("OK",4));
                break;
            case R.id.tvUnQualified:
                showSubmitDialog(() -> requestQC("NG",4));
                break;
            case R.id.tvBaseSubmit:
                showActivity();
                break;
            case R.id.CLQualified:
                showSubmitDialog(() -> requestQC("OK",1));
                break;
            case R.id.CLUnQualified:
                showSubmitDialog(() -> requestQC("NG",1));
                break;
            case R.id.TLLQualified:
                showSubmitDialog(() -> requestQC("OK",2));
                break;
            case R.id.TLLUnQualified:
                showSubmitDialog(() -> requestQC("NG",2));
                break;
            case R.id.DKQualified:
                if (rbYes.isChecked() || rbNo.isChecked()){
                    showSubmitDialog(() -> requestQC("OK",3));
                }else {
                    showToast("请选择是否已接收样品",false);
                    return;
                }
                break;
            case R.id.DKUnQualified:
                if (rbYes.isChecked() || rbNo.isChecked()){
                    showSubmitDialog(() -> requestQC("NG",3));
                }else {
                    showToast("请选择是否已接收样品",false);
                    return;
                }
                break;
            case R.id.WGQualified:
                showSubmitDialog(() -> requestQC("OK",1));
                break;
            case R.id.WGUnQualified:
                showSubmitDialog(() -> requestQC("NG",1));
                break;
            case R.id.rbYes:
            case R.id.rbNo:
                if (initialSetValue){
                    requestQC("",5);
                }
                break;
        }
    }

    private void showActivity() {
        Bundle bundle = new Bundle();
        bundle.putString("FROM","QC");
        bundle.putSerializable("REPAIRFROM",mData);
        switch (tvRepairType.getText().toString()){
            case "一般故障":
            case "设备故障":
            case "程序问题":
                FreeApi_Utils.skipWithExtraIntent("header",bundle,this,FaultRegisterActivity.class);
                break;
            case "换产品":
                FreeApi_Utils.skipWithExtraIntent("header",bundle,this, ReplaceProductActivity.class);
                break;
            case "换材料":
                FreeApi_Utils.skipWithExtraIntent("header",bundle,this, ReplaceMaterialActivity.class);
                break;
            case "换Tooling":
                FreeApi_Utils.skipWithExtraIntent("header",bundle,this,ReplaceToolActivity.class);
                break;
        }
    }

    private String getSData(){
        Date currentDate = new Date();

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.getDefault());

        String formattedDate = dateFormat.format(currentDate);
        return formattedDate;
    }


    private void requestQC(String result,int type) {
        FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<JSONObject>>
                (new TypeToken<BaseResponseBean<JSONObject>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                    showConfirmDialog(QCConfirmActivity.this, "提交成功");
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<JSONObject>> response) {
                super.onError(response);
                loadError(response.getException(), "PMSREPAIR");

            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE","QC");
        map.addProperty("COMMENTS",getText(etComment));
        map.addProperty("REPAIRTYPE",mData.getREPAIRSHEETDATA().getREPAIRTYPE());
        map.addProperty("QC","Y");
//        map.addProperty(DATE1,getSData());
//        map.addProperty(OKRESULT1,result);
//        map.addProperty("QCRESULT",result);
        mData.getREPAIRFORM().setDATE1("");
        mData.getREPAIRFORM().setOKRESULT1("");
        mData.getREPAIRFORM().setDATE2("");
        mData.getREPAIRFORM().setOKRESULT2("");
        mData.getREPAIRFORM().setDATE3("");
        mData.getREPAIRFORM().setOKRESULT3("");
        if (type==1){
            mData.getREPAIRFORM().setDATE1(getSData());
            mData.getREPAIRFORM().setOKRESULT1(result);
        } else if (type==2) {
            mData.getREPAIRFORM().setDATE2(getSData());
            mData.getREPAIRFORM().setOKRESULT2(result);
        }else if (type==3) {
            mData.getREPAIRFORM().setDATE3(getSData());
            mData.getREPAIRFORM().setOKRESULT3(result);
        } else if (type==4) {
            mData.getREPAIRFORM().setDATE1(getSData());
            mData.getREPAIRFORM().setOKRESULT1(result);
            mData.getREPAIRFORM().setDATE2(getSData());
            mData.getREPAIRFORM().setOKRESULT2(result);
            mData.getREPAIRFORM().setDATE3(getSData());
            mData.getREPAIRFORM().setOKRESULT3(result);
        } else if (type==5) {
            mData.getREPAIRFORM().setSAMPLEFLAGE(rbYes.isChecked()?"Y":"N");
        }

        map.add("REPAIRFORM", new JsonParser().parse(new Gson().toJson(mData.getREPAIRFORM())).getAsJsonObject());
        map.add("REPAIRSHEETDATA", new JsonParser().parse(new Gson().toJson(mData.getREPAIRSHEETDATA())).getAsJsonObject());
        NetUtils.requestPostNet("IF.PMSREPAIR", "PMSREPAIR", QCConfirmActivity.this, map, callBack);
    }
}